<?php

namespace app\admin\controller\comment;

use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use app\admin\model\UM as UM;
use app\admin\service\CommentService;
use think\APP;
use think\Request as Request;

/**
 * Comment 控制器
 * Class Comment
 * @package app\admin\controller\comment;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\admin\validate\comment\Comment
 */
class Comment
{
    // 请求参数
    private Request $request;

    // service
    private CommentService $service;

    public function __construct(Request $request, CommentService $commentService)
    {
        $this->request = $request;
        $this->service = $commentService;
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /comment/comment/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request     = $this->request;

        $startTime = $request->param('startTime', null);
        $endTime = $request->param('endTime', null);
        $kw = $request->param('kw', null);

        $page      = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->index($user, $startTime, $endTime, $kw, $page, $page_size));
    }
    //
    // /**
    //  * 详情
    //  * @param UM|null $user
    //  * @return ApiResponse
    //  * @url /comment/comment/read
    //  */
    // public function read(?UM $user): ApiResponse
    // {
    //     $request     = $this->request;
    //
    //     $id = $request->param('id');
    //
    //     if(!CheckUtil::isId($id)){
    //         return ApiResponse::fail('缺少id参数');
    //     }
    //
    //     return ApiResponse::success($this->service->read($user, $id));
    // }
    //
    // /**
    //  * 新增|修改
    //  * @param UM $user
    //  * @return ApiResponse
    //  * @url /comment/comment/save
    //  */
    // public function save(UM $user): ApiResponse
    // {
    //     $request     = $this->request;
    //
    //     if (!$request->has('id')) {
    //         $data = $this->service->add($user, $request->param());
    //     } else {
    //         $data = $this->service->modify($user, $request->param('id'), $request->param());
    //     }
    //
    //     return ApiResponse::success($data);
    // }

    /**
     * 删除
     * @param UM $user
     * @return ApiResponse
     * @url /comment/comment/delete
     */
    public function delete(UM $user): ApiResponse
    {
        $request     = $this->request;

        $id = $request->param('id');

        if(!CheckUtil::isId($id)){
            return ApiResponse::fail('缺少id参数');
        }

        $this->service->rm($user, $id);

        return ApiResponse::success(null);
    }
}
